package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.RoleMapper;
import com.bsh.mybatis.model.Role;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Repository
public class DaoRole {
	@Resource
	SqlSessionTemplate session;
	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	
	RoleMapper getMapper(){
		return session.getMapper(RoleMapper.class);
	}
		
	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(Role record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(Role record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public Role selectByPrimaryKey(Integer id) {
		Role row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(Role record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(Role record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	/**
	 * 角色列表分页
	 */
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("SELECT * from role ");
		sb.append("where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		sb.append(" order by sort desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}	
	
	/**
	 * 添加角色
	 * @param roleid
	 * @return
	 */
	public Integer addRole(Role role){
		RoleMapper map = this.getMapper();
		return map.insert(role);
	}
	
	/**
	 * 修改角色
	 * @param roleid
	 * @return
	 */
	public Integer editRole(Role role){		
		RoleMapper map = this.getMapper();
		return map.editRole(role);
	}
	
	/**
	 * 禁用角色
	 * @param roleid
	 * @return
	 */
	public Integer enableRole(int id,int isEnabele){		
		RoleMapper map = this.getMapper();
		Integer res = map.enableRole(id, isEnabele);
		return res;
	}	

	/**
	 * 查看所有角色
	 * @param roleid
	 * @return
	 */
	public List<Role> getRoles(){		
		RoleMapper map = this.getMapper();
		List<Role> res = map.getRoles();
		return res;
	}
	
	/**
	 * 查看管理员的角色
	 * @param roleid
	 * @return
	 */
	public List<Role> getAdminRoles(int adminId){		
		RoleMapper map = this.getMapper();
		List<Role> res = map.getAdminRoles(adminId);
		return res;
	}
}
